<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link type="text/css" rel="stylesheet" href="../css/layui.css" />
		<script type="text/javascript" src="../layui.js"></script>
	</head>
	<body>
		<div id="test1"></div>
		<div id="test2"></div>
		<script>
		layui.use(['transfer'],function(){
			var transfer=layui.transfer,$=layui.$;
            var rols = new Array;
			$.post(
				"http://stuapi.ysdjypt.com/api/GetRolesByUserIdLayUI",
				{
					"uid":"90de27cf-d186-45f0-929b-f179e94c9b4a",
					"token":"23d66d84-fae1-45d5-bd07-b694662fe579"
				},
				function(res){
					for (var i = 0; i < res.data.length; i++) {
						rols.push(res.data[i].Id);
					}
				},
				"json"
			);
			
			//请求拿数据
			$.post(
			   'http://stuapi.ysdjypt.com/api/GetRolesAll',
			   {'token':'23d66d84-fae1-45d5-bd07-b694662fe579'},
			   function(data){
				   transfer.render({
					   elem:'#test2',
					   title:['系统角色','当前用户角色'],
					   data:data,
					   value:rols,
					   parseData:function(res){
						   return{
							   "value":res.Id,
							   "title":res.Name
							   }
						   },
						   id:"testT",
						   onchange:function(obj,index){
								console.log(obj);
								console.log(index);
								// 0  从左到右  修改
								// 1  从右往左  删除
								if(index == 0){
									layer.alert("移动修改的内容"+obj);
									//修改角色地址
									for (var i = 0; i < obj.length; i++) {
										$.post(
										'http://stuapi.ysdjypt.com/api/AddUserToRole',
										{
										'uId':data.Id,
										'rId':obj[i].value,
										'token':'23d66d84-fae1-45d5-bd07-b694662fe579'
										},function(res){
										},'json');
									}
								}else{
									layer.alert("删除的内容"+obj);
									//更新角色地址
									for (var i = 0; i < obj.length; i++) {
										$.post(
										'http://stuapi.ysdjypt.com/api/RemoveUserFromRole',
										{
										'uId':data.Id,
										'rId':obj[i].value,
										'token':'23d66d84-fae1-45d5-bd07-b694662fe579'
										},function(res){
										},'json');
									}
								}
								
							}
				   });
			   },'json');
			});
		</script>
	</body>
</html>
